package com.beeasy.hzbpm.service;

import com.github.llyb120.nami.core.MultipartFile;
import com.github.llyb120.json.Obj;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import static com.beeasy.hzbpm.bean.MongoService.db;
/**
 * @Author: Stunievi
 * @Date: 2019/12/27 16:09
 */
public class msgService {
    /**
     * 发送消息
     * */
    public void sendMsg(Obj info){
        String id = String.valueOf(ObjectId.get());
        info.put("id",id);
//        System.err.println(info);
        db.getCollection("system_notice")
                .insertOne(info.toBson());
//        return null;
    }
    public static Integer random(){
        Random r = new Random();
        int ran1 = 0;
        for(int i = 0; i< 10; i++){
            ran1 += r.nextInt(999);
        }
//        System.out.println(new Date().getTime());
//        System.out.println(ran1);
        return ran1;
    }
    /**
     * 获取文件名
     * */
    public  static String getFileName(String fileNames){
        String fileName = fileNames;
//判断是否为IE浏览器的文件名，IE浏览器下文件名会带有盘符信息
// Check for Unix-style path
        int unixSep = fileNames.lastIndexOf('/');
// Check for Windows-style path
        int winSep = fileNames.lastIndexOf('\\');
// Cut off at latest possible point
        int pos = (winSep > unixSep ? winSep : unixSep);
        if (pos != -1)  {
            // Any sort of path separator found...
            fileName = fileNames.substring(pos + 1);
        }
        return fileName;
    }
    // 判断文件是否存在
    public static String isExist(String updoadPath,String fileName){
        // 获取文件名

        String filePath = updoadPath + "/" + fileName;
//                System.out.println(filePath);
        File desFile = new File(filePath);
        // 判断文件名是否存在
//        System.out.println(desFile.exists());
        if(desFile.exists()) {
            // 文件存在
            return isExist(updoadPath,String.valueOf(random()) + fileName);
//            filePath = UPLOAD_PATH + "/" + fileName + String.valueOf(random());
//            desFile = new File(filePath);
//            throw new RuntimeException("文件存在");
        }
        return fileName;
    }
    /**
     *  保存文件并写入数据库
     *
     * */
    public ArrayList uploadFiles(MultipartFile f, String UPLOAD_PATH) throws UnsupportedEncodingException {
        ArrayList al = new ArrayList();

        if(f.length() != 0){
            // 获取文件名
            String fileName = isExist(UPLOAD_PATH,getFileName(f.fileName));
            fileName = URLDecoder.decode(fileName, "UTF-8");

            String filePath = UPLOAD_PATH + "/" + fileName;
            File desFile = new File(filePath);
            // 判断文件夹是否存在
            File dir = new File(UPLOAD_PATH);
            if(dir.exists()){
                System.err.println("路径存在");
            }else{
                System.err.println("路径不存在，创建文件夹");
                dir.mkdirs();
            }
            String date = String.valueOf(new Date().getTime()/1000);
            try {
                f.transferTo(desFile);
                // 存入
                Document dm = new Document("path",filePath);
                dm.append("date",date);
                dm.append("name",fileName);
                dm.append("id", String.valueOf(ObjectId.get()));

                db.getCollection("saveFiles").insertOne(dm);


                al.add(dm);
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();

            }
        }

        return al;
    }

}
